
/**
 * Item View
 */
window.ItemAddView = Backbone.View.extend({
 	initialize: function() {
 		this.render();
 	},
 	render: function() {
		var model = this.model.toJSON();
 		$(this.el).html(this.template(model));
 		return this;
 	}
});

window.ItemUploadView = Backbone.View.extend({
	initialize: function() {
		this.render();
	},
	render: function() {
		var self = this;
		
		$(self.el).html(self.template());
		self.uploadFile();
		return self;
	},
	
	uploadFile: function(e) {
		var self = this;
		
		var options = {
			beforeSend: function(xhr, opts) {
				$('.progress').removeClass('hide');
				$('.progress').addClass('active').show();
				
				$('#message').html('');				
			},
			uploadProgress: function(event, position, total, percentComplete) {					
				$('.progress-bar').css('width',percentComplete+'%');							
			},
			complete: function(response) {				
				$('.progress').removeClass('active progress-striped');
				$('#upload-form').fadeIn(3000);
			}, 
			success: function() {
				$('#message').addClass('alert-success').html('Successfully your file is uploaded !!');
			},
			error: function(error) {
				var errorMessage = JSON.parse(error.responseText);
				
				$('.progress').hide();
				$('#message').addClass('alert-danger').html('Error: '+errorMessage.message);
			}
		};
		
		$('#upload-form',$(self.el)).submit(function() { 
			
			//Validate the form			
			var valid = ($('#name').val() != "") && ($('#myFile').val() != "");
			
			if(valid) {
				$(this).hide();
				$(this).ajaxSubmit(options);
			} else {
				$('#message').addClass('alert-danger').html('The name and file selection is mandatory');
			}
			
	        // !!! Important !!! 
	        // always return false to prevent standard browser submit and page navigation 
	        return false; 
	    });
	}
});